package cn.ivicar.app.resource.vehicle;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.codehaus.plexus.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;
import com.google.inject.servlet.RequestScoped;

import cn.com.carsmart.exception.BadRequestException;
import cn.com.carsmart.exception.CarsmartException;
import cn.com.carsmart.exception.InternalErrorException;
import cn.com.carsmart.exception.NotAuthorizedException;
import cn.com.carsmart.exception.util.ExceptionResponseBuilder;
import cn.com.carsmart.sae.annotation.RequiresAuthentication;
import cn.com.carsmart.ws.util.CacheUtil;
import cn.ivicar.app.controller.sae.CurrentUtil;
import cn.ivicar.app.controller.vehicle.PositionController;
import cn.ivicar.app.controller.vehicle.VehicleController;
import net.sf.json.JSONObject;

@Path("vehicles")
@RequestScoped
public class VehicleReource {

	private static transient final Logger LOGGER = LoggerFactory.getLogger(VehicleReource.class);

	@Inject
	private PositionController positionController;
	@Inject
	private VehicleController vehicleController;

	/**
	 * 请求车辆位置
	 * 
	 * @return
	 */
	@RequiresAuthentication
	@GET
	@Path("{vehicleId}/location")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Response location(@PathParam("vehicleId") String vehicleIdStr) {
		try {
			if (StringUtils.isBlank(vehicleIdStr)) {
				throw new BadRequestException("400", "请求参数vehicleId不能为空!");
			}
			Long vehicleId = Long.valueOf(vehicleIdStr);
			Long customerId = CurrentUtil.getCustomerId();
			JSONObject result = positionController.getVehicleLocation(vehicleId, customerId);
			return CacheUtil.noCache(Response.ok(result)).build();
		} catch (NotAuthorizedException e) {
			LOGGER.info("请求车辆位置信息出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (BadRequestException e) {
			LOGGER.info("请求车辆位置信息出错:" + e.getMessage());
			return ExceptionResponseBuilder.build(e);
		} catch (InternalErrorException e) {
			LOGGER.error("请求车辆位置信息出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (CarsmartException e) {
			LOGGER.error("请求车辆位置信息出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (Exception e) {
			LOGGER.error("请求车辆位置信息出错:", e);
			return ExceptionResponseBuilder.build(new InternalErrorException(e));
		}
	}

	/**
	 * 请求车辆基本信息
	 * 
	 * @return
	 */
	@RequiresAuthentication
	@GET
	@Path("{vehicleId}")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Response getVehicleInfo(@PathParam("vehicleId") String vehicleIdStr) {
		try {
			if (StringUtils.isBlank(vehicleIdStr)) {
				throw new BadRequestException("400", "请求参数vehicleId不能为空!");
			}
			Long vehicleId = Long.valueOf(vehicleIdStr);
			Long customerId = CurrentUtil.getCustomerId();
			JSONObject result = vehicleController.getVehicleInfo(vehicleId, customerId);
			return CacheUtil.noCache(Response.ok(result)).build();
		} catch (NotAuthorizedException e) {
			LOGGER.info("请求车辆基本信息出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (BadRequestException e) {
			LOGGER.info("请求车辆基本信息出错:" + e.getMessage());
			return ExceptionResponseBuilder.build(e);
		} catch (InternalErrorException e) {
			LOGGER.error("请求车辆基本信息出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (CarsmartException e) {
			LOGGER.error("请求车辆基本信息出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (Exception e) {
			LOGGER.error("请求车辆基本信息出错:", e);
			return ExceptionResponseBuilder.build(new InternalErrorException(e));
		}
	}

	/**
	 * 设置车辆基本信息
	 * 
	 * @return
	 */
	@RequiresAuthentication
	@PUT
	@Path("{vehicleId}")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Response setVehicleInfo(@PathParam("vehicleId") String vehicleIdStr, JSONObject params) {
		try {
			if (StringUtils.isBlank(vehicleIdStr)) {
				throw new BadRequestException("400", "请求参数vehicleId不能为空!");
			}

			Long vehicleId = Long.valueOf(vehicleIdStr);
			Long customerId = CurrentUtil.getCustomerId();
			vehicleController.setVehicleInfo(vehicleId, params, customerId);
			return CacheUtil.noCache(Response.ok()).build();
		} catch (NotAuthorizedException e) {
			LOGGER.info("设置车辆基本信息出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (BadRequestException e) {
			LOGGER.info("设置车辆基本信息出错:" + e.getMessage());
			return ExceptionResponseBuilder.build(e);
		} catch (InternalErrorException e) {
			LOGGER.error("设置车辆基本信息出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (CarsmartException e) {
			LOGGER.error("设置车辆基本信息出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (Exception e) {
			LOGGER.error("设置车辆基本信息出错:", e);
			return ExceptionResponseBuilder.build(new InternalErrorException(e));
		}
	}

	/**
	 * 车辆绑定
	 * 
	 * @return
	 */
	@RequiresAuthentication
	@POST
	@Path("bind")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Response bind(JSONObject params) {
		try {
			Long customerId = CurrentUtil.getCustomerId();
			vehicleController.bind(customerId, params);
			return CacheUtil.noCache(Response.ok()).build();
		} catch (NotAuthorizedException e) {
			LOGGER.info("车辆绑定出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (BadRequestException e) {
			LOGGER.info("车辆绑定出错:" + e.getMessage());
			return ExceptionResponseBuilder.build(e);
		} catch (InternalErrorException e) {
			LOGGER.error("车辆绑定出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (CarsmartException e) {
			LOGGER.error("车辆绑定出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (Exception e) {
			LOGGER.error("车辆绑定出错:", e);
			return ExceptionResponseBuilder.build(new InternalErrorException(e));
		}
	}

	/**
	 * 车辆解绑
	 * 
	 * @return
	 */
	@RequiresAuthentication
	@PUT
	@Path("{vehicleId}/unbind")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Response unbind(@PathParam("vehicleId") String vehicleIdStr) {
		try {
			Long vehicleId = Long.valueOf(vehicleIdStr);
			Long customerId = CurrentUtil.getCustomerId();
			vehicleController.unbind(customerId, vehicleId);
			return CacheUtil.noCache(Response.ok()).build();
		} catch (NotAuthorizedException e) {
			LOGGER.info("车辆解绑出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (BadRequestException e) {
			LOGGER.info("车辆解绑出错:" + e.getMessage());
			return ExceptionResponseBuilder.build(e);
		} catch (InternalErrorException e) {
			LOGGER.error("车辆解绑出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (CarsmartException e) {
			LOGGER.error("车辆解绑出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (Exception e) {
			LOGGER.error("车辆解绑出错:", e);
			return ExceptionResponseBuilder.build(new InternalErrorException(e));
		}
	}

	/**
	 * 请求绑定车辆列表
	 * 
	 * @return
	 */
	@RequiresAuthentication
	@GET
	@Path("binds")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Response getBindVehicleList() {
		try {
			Long customerId = CurrentUtil.getCustomerId();
			JSONObject result = vehicleController.getBindVehicleList(customerId);
			return CacheUtil.noCache(Response.ok(result)).build();
		} catch (NotAuthorizedException e) {
			LOGGER.info("请求绑定车辆列表出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (BadRequestException e) {
			LOGGER.info("请求绑定车辆列表出错:" + e.getMessage());
			return ExceptionResponseBuilder.build(e);
		} catch (InternalErrorException e) {
			LOGGER.error("请求绑定车辆列表出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (CarsmartException e) {
			LOGGER.error("请求绑定车辆列表出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (Exception e) {
			LOGGER.error("请求绑定车辆列表出错:", e);
			return ExceptionResponseBuilder.build(new InternalErrorException(e));
		}
	}

	/**
	 * 车辆共享
	 * 
	 * @return
	 */
	@RequiresAuthentication
	@POST
	@Path("{vehicleId}/share")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Response share(@PathParam("vehicleId") String vehicleIdStr, JSONObject params) {
		try {
			Long vehicleId = Long.valueOf(vehicleIdStr);
			Long customerId = CurrentUtil.getCustomerId();

			vehicleController.share(customerId, vehicleId, params);
			return CacheUtil.noCache(Response.ok()).build();
		} catch (NotAuthorizedException e) {
			LOGGER.info("车辆共享出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (BadRequestException e) {
			LOGGER.info("车辆共享出错:" + e.getMessage());
			return ExceptionResponseBuilder.build(e);
		} catch (InternalErrorException e) {
			LOGGER.error("车辆共享出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (CarsmartException e) {
			LOGGER.error("车辆共享出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (Exception e) {
			LOGGER.error("车辆共享出错:", e);
			return ExceptionResponseBuilder.build(new InternalErrorException(e));
		}
	}

	/**
	 * 解除车辆共享
	 * 
	 * @return
	 */
	@RequiresAuthentication
	@PUT
	@Path("{vehicleId}/unshare")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Response unVehicleshare(@PathParam("vehicleId") String vehicleIdStr, JSONObject params) {
		try {
			Long vehicleId = Long.valueOf(vehicleIdStr);
			Long customerId = CurrentUtil.getCustomerId();
			vehicleController.unVehicleshare(customerId, vehicleId, params);
			return CacheUtil.noCache(Response.ok()).build();
		} catch (NotAuthorizedException e) {
			LOGGER.info("解除车辆共享出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (BadRequestException e) {
			LOGGER.info("解除车辆共享出错:" + e.getMessage());
			return ExceptionResponseBuilder.build(e);
		} catch (InternalErrorException e) {
			LOGGER.error("解除车辆共享出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (CarsmartException e) {
			LOGGER.error("解除车辆共享出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (Exception e) {
			LOGGER.error("解除车辆共享出错:", e);
			return ExceptionResponseBuilder.build(new InternalErrorException(e));
		}
	}

	/**
	 * 请求车辆共享信息
	 * 
	 * @return
	 */
	@RequiresAuthentication
	@GET
	@Path("{vehicleId}/shares")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Response getVehicleShareList(@PathParam("vehicleId") String vehicleIdStr) {
		try {
			Long vehicleId = Long.valueOf(vehicleIdStr);
			Long customerId = CurrentUtil.getCustomerId();
			JSONObject result = vehicleController.getVehicleShareList(vehicleId, customerId);
			return CacheUtil.noCache(Response.ok(result)).build();
		} catch (NotAuthorizedException e) {
			LOGGER.info("请求车辆共享信息出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (BadRequestException e) {
			LOGGER.info("请求车辆共享信息出错:" + e.getMessage());
			return ExceptionResponseBuilder.build(e);
		} catch (InternalErrorException e) {
			LOGGER.error("请求车辆共享信息出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (CarsmartException e) {
			LOGGER.error("请求车辆共享信息出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (Exception e) {
			LOGGER.error("请求车辆共享信息出错:", e);
			return ExceptionResponseBuilder.build(new InternalErrorException(e));
		}
	}
	
	/**
	 * 请求被分享车辆列表
	 * 
	 * @return
	 */
	@RequiresAuthentication
	@GET
	@Path("shared")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Response getSharedVehicleList() {
		try {
			Long customerId = CurrentUtil.getCustomerId();
			JSONObject result = vehicleController.getSharedVehicleList(customerId);
			return CacheUtil.noCache(Response.ok(result)).build();
		} catch (NotAuthorizedException e) {
			LOGGER.info("请求被分享车辆列表出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (BadRequestException e) {
			LOGGER.info("请求被分享车辆列表出错:" + e.getMessage());
			return ExceptionResponseBuilder.build(e);
		} catch (InternalErrorException e) {
			LOGGER.error("请求被分享车辆列表出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (CarsmartException e) {
			LOGGER.error("请求被分享车辆列表出错:", e);
			return ExceptionResponseBuilder.build(e);
		} catch (Exception e) {
			LOGGER.error("请求被分享车辆列表出错:", e);
			return ExceptionResponseBuilder.build(new InternalErrorException(e));
		}
	}

}